<?php
class FindPwd extends CComponent {

	public $memberModel;
    public $member;
    public $findModel = 'FindPassword';
    public $memberNameAttr;
    public $emailAttr;
    public $criteria = array();

    public function init() {
        $this->member = new $this->memberModel;
    }

    /**
     * 创建找回密码记录
     * Enter description here ...
     */
    protected function create() {
    	$model = new $this->findModel;
    	$token = $this->createToken(CHtml::value($this->member, $this->memberNameAttr));
    	
    	$crieteria = new CDbCriteria();
    	$crieteria->compare('token', $token);
    	$crieteria->compare('state', '0');
    	$model->updateAll(array('state'=>1),$crieteria);
    	
    	$attributes = array(
    		'uid' => $this->member->id,
    		'name' => CHtml::value($this->member, $this->memberNameAttr),
    		'email' => CHtml::value($this->member, $this->emailAttr),
    		'token' => $token,
    		'time' => date('Y-m-d H:i:s'),
    		'state' => 0,
    	);
    	$model->attributes = $attributes;
       	
      	return $model->save();
    }
    
    /**
     * 判断用户是否存在，存在创建找回密码记录
     * Enter description here ...
     * @param unknown_type $username
     * @param unknown_type $email
     */
    public function run($username,$email) {
    	if (empty($username) || empty($email)) {
    		 Yii::app()->user->setFlash('findPwd','用户名和邮箱不能为空');
            return false;
    	}
    	
    	$this->findMember($username, $email);
    	
        if(!is_object($this->member)){
            Yii::app()->user->setFlash('findPwd','对不起，您输入的账户或邮箱有误，请重新输入');
            return false;
        } else {
            if($this->create()) {
                $this->sendEmail();
                 Yii::app()->user->setFlash('findPwd','邮件已发送，请查收');
            	return true;
            }
        }
    }

   	/**
   	 * 查找用户
   	 * Enter description here ...
   	 * @param unknown_type $username
   	 * @param unknown_type $email
   	 */
    protected function findMember($username, $email) {
    	$criteria = new CDbCriteria($this->criteria);
    	$criteria->compare($this->memberNameAttr, $username);
    	$criteria->compare($this->emailAttr, $email);
    	$this->member = $this->member->find($criteria);
    } 

    /**
     * 创建修改密码地址
     * Enter description here ...
     * @param objcet $member
     */
    public function createReturnUrl($member) {
        return 'http://'.$_SERVER['SERVER_NAME']
        	.Yii::app()->createUrl('misc/resetPwd',array('token'=>$this->createToken(CHtml::value($member, $this->memberNameAttr))));
    }
    
    /**
     * 修改状态
     * Enter description here ...
     * @param unknown_type $token
     */
    public function updateState($token) {
    	$model = new $this->findModel;
    	$model = $model->findByAttributes(array('token'=>$token,'state'=>0));
    	if($model) {
    		$model->state = 1;
    		return $model->save();
    	}
    	return false;
    }
    
    /**
     * 创建密钥
     * Enter description here ...
     * @param unknown_type $workerName
     */
    public function createToken($workerName) {
        return md5($workerName);
    }

    /**
     * 发送邮件
     * Enter description here ...
     * @param unknown_type $member
     */
    protected function sendEmail() {
        $content = EmailTemplate::getInstance('updatepwd')->getTemplateContent($this->member);;
        SendMailService::sendMail($content, '驴友村', CHtml::value($this->member, $this->emailAttr));
    }

}
?>